// Copyright (c) 2023 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This is an example of using the `ylong_xml` crate to parse an XML document.
//!
//! Here we are parsing an XML string, retrieving its document element and
//! child nodes, and checking the name and value of these elements.

use ylong_xml::{
    Document, DocumentMethod, Encoding, NodeMethod, ParentNodeMethod, ValuedNodeMethod,
};

const XML_TEXT: &str = r#"
<?xml version="1.0" encoding="UTF-8"?>
<message>
    <warning>
         Hello World
    </warning>
</message>
"#;

fn main() {
    let document = Document::from_text(XML_TEXT).unwrap();
    assert_eq!(document.version(), "1.0");
    assert_eq!(document.xml_encoding(), Some(Encoding::Utf8));

    let element = document.document_element().unwrap();
    assert_eq!(element.name(), "message");

    let child_node_0 = element.children().item(0).unwrap();
    let child_element = child_node_0.as_element().unwrap();
    assert_eq!(child_element.name(), "warning");

    let child_node_text = child_element.children().item(0).unwrap();
    let text = child_node_text.as_text().unwrap();
    assert_eq!(text.value(), "Hello World");
}
